package org.song.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.LogRecord;
import org.song.annotation.PowerEquals;
import org.song.annotation.RequestTimeRecord;
import org.song.exception.result.AjaxResult;
import org.song.model.dto.TypeDto;
import org.song.model.vo.Type;
import org.song.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 博客类型控制层
 */
@Api(tags = "博客类型控制层")
@Controller
@RequestMapping(value = "/type")
public class TypeController {

    // Autowired默认的是ByType按照类型进行从spring容器中依赖注入的，如果有多个类型则按照name进行依赖注入
    // Resource 是JDK的自带的API 默认是按照Name方式进行依赖注入的
    @Autowired( required = true )
    @Qualifier( value = "typeServiceImpl")
    private TypeService typeService;

    /**
     * 查询所有的博客类型
     * @param typeDto
     * @return
     */
    @RequestTimeRecord
    @ApiOperation(value = "查询所有的博客类型" , notes = "查询所有的博客类型")
    @ResponseBody
    @RequestMapping(value = "/listTypes" , method = RequestMethod.GET)
    public AjaxResult<PageInfo<Type>> listTypes(@ApiParam(value = "typeDto实例接受查询参数") @NonNull TypeDto typeDto) {
        // 开启分页 PageInfo的分页方法是基于前置拦截 完成的，底层是通过字符串拼接方式拼接上分页参数
        // 最近一次的sql执行会自动拼接到sql语句中去
        PageHelper.startPage(typeDto.getPage() , typeDto.getSize());
       return AjaxResult.success(new PageInfo<>(typeService.listTypes(typeDto)));
    }

    /**
     * 删除博客类型
     * @param type
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "删除博客类型" , notes = "删除博客类型")
    @ResponseBody
    @RequestMapping(value = "/deleteType" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteType(@ApiParam(value = "接受需要删除的博客类型") @NonNull  Type type) {
        return AjaxResult.success(typeService.deleteType(type));
    }

    /**
     * 增加博客类型
     * @param type
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "增加博客类型" , notes = "增加博客类型")
    @ResponseBody
    @RequestMapping(value = "/insertType" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertType(@ApiParam(value = "博客类型") @NonNull @RequestBody Type type) {
        return AjaxResult.success(typeService.insertType(type));
    }

    /**
     * 更新博客类型
     * @param type
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "更新博客类型" , notes = "更新博客类型")
    @ResponseBody
    @RequestMapping(value = "/updateType" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateType(@ApiParam(value = "根据传入的类型信息进行博客类型更新") @NonNull @RequestBody Type type) {
        return AjaxResult.success(typeService.updateType(type));
    }


}
